package cn.com.qiter.educms.controller;

import cn.com.qiter.common.Result;
import cn.com.qiter.educms.entity.CmsDictionary;
import cn.com.qiter.educms.service.CmsDictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 公用字典表 前端控制器
 * </p>
 *
 * @author 鲁仁华
 * @since 2021-03-06
 */
@RestController
    @RequestMapping("/educms/dictionary")
public class CmsDictionaryController {

    @Autowired
    private CmsDictionaryService service;

    //1 分页查询banner
    @GetMapping("pageDict/{page}/{limit}")
    public Result pageDict(@PathVariable long page, @PathVariable long limit) {
        Page<CmsDictionary> pageBanner = new Page<>(page,limit);
        service.page(pageBanner,null);
        return Result.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    //2 添加banner
    @PostMapping("addDict")
    public Result addDict(@RequestBody CmsDictionary dictionary) {
        service.save(dictionary);
        return Result.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public Result get(@PathVariable String id) {
        CmsDictionary banner = service.getById(id);
        return Result.ok().data("item", banner);
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("getByName/{name}")
    public Result getByName(@PathVariable String name) {
        QueryWrapper<CmsDictionary> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        List<CmsDictionary> banner = service.list(wrapper);
        return Result.ok().data("items", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public Result updateById(@RequestBody CmsDictionary banner) {
        service.updateById(banner);
        return Result.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable String id) {
        service.removeById(id);
        return Result.ok();
    }
}

